VPython Class 2: Functions, Fields, and the dreaded ˆr

Similar documents
Computer Model of Spring-Mass System. Q1: Can a computer model of a mass-spring system, based on the finite-time form of the momentum principle

1 Newton s 2nd and 3rd Laws

Math Lab 8: Electric Fields Integrating Continuous Charge Distributions II Due noon Thu. Feb. 1 in class

Union College Winter 2013

Calculating and displaying the magnetic field of a single moving charged particle

Math Lab 10: Differential Equations and Direction Fields Complete before class Wed. Feb. 28; Due noon Thu. Mar. 1 in class

Lab 2 Worksheet. Problems. Problem 1: Geometry and Linear Equations

Calculations to predict motion or move objects (done repetitively in a loop)

CS1110 Lab 3 (Feb 10-11, 2015)

Exploring Graphs of Polynomial Functions

Relative Photometry with data from the Peter van de Kamp Observatory D. Cohen and E. Jensen (v.1.0 October 19, 2014)

Computer Model of Spring-Mass System, Part 3 of Springs Lab

To factor an expression means to write it as a product of factors instead of a sum of terms. The expression 3x

HR Diagram of Globular Cluster Messier 80 Using Hubble Space Telescope Data

Problem Solving 1: The Mathematics of 8.02 Part I. Coordinate Systems

#29: Logarithm review May 16, 2009

The Monte Carlo Method

Overview In chapter 16 you learned how to calculate the Electric field from continuous distributions of charge; you follow four basic steps.

Matter & Interactions I Fall 2016

Sums of Squares (FNS 195-S) Fall 2014

Vectors. Vector Practice Problems: Odd-numbered problems from

LAB Exercise #4 - Answers The Traction Vector and Stress Tensor. Introduction. Format of lab. Preparation reading

Physics 212E Spring 2004 Classical and Modern Physics. Computer Exercise #2

Solve Systems of Equations Algebraically

Data Structures & Database Queries in GIS

Making Measurements. On a piece of scrap paper, write down an appropriate reading for the length of the blue rectangle shown below: (then continue )

Tutorial Three: Loops and Conditionals

CHARTING THE HEAVENS USING A VIRTUAL PLANETARIUM

Tutorial 8 Raster Data Analysis

Your Second Physics Simulation: A Mass on a Spring

Math 308 Midterm Answers and Comments July 18, Part A. Short answer questions

TEACHER NOTES MATH NSPIRED

Vectors. A vector is usually denoted in bold, like vector a, or sometimes it is denoted a, or many other deviations exist in various text books.

Lab Exercise 03: Gauss Law

Lab 1: Empirical Energy Methods Due: 2/14/18

How to write maths (well)

LINEAR ALGEBRA KNOWLEDGE SURVEY

Lesson 3-1: Solving Linear Systems by Graphing

Create Satellite Image, Draw Maps

Modeling the Motion of a Projectile in Air

CE 365K Exercise 1: GIS Basemap for Design Project Spring 2014 Hydraulic Engineering Design

2.1 Definition. Let n be a positive integer. An n-dimensional vector is an ordered list of n real numbers.

A GUI FOR EVOLVE ZAMS

Projects in Geometry for High School Students

Experiment 1: The Same or Not The Same?

One sided tests. An example of a two sided alternative is what we ve been using for our two sample tests:

Exercises for Windows

THAT KEEP ESCAPING THE CASE OF THE CENTIGURPS THAT KEEP ESCAPING. 10s THAT KEEP ESCAPING THAT KEEP ESCAPING. THE CASE OF The CENTIGURPS

You will be writing code in the Python programming language, which you may have learnt in the Python module.

Calculating Bond Enthalpies of the Hydrides

Alex s Guide to Word Problems and Linear Equations Following Glencoe Algebra 1

GMS 8.0 Tutorial MT3DMS Advanced Transport MT3DMS dispersion, sorption, and dual domain options

LAB 2 - ONE DIMENSIONAL MOTION

Lesson 5b Solving Quadratic Equations

Electric Fields and Equipotentials

Main topics for the First Midterm Exam

Nondeterministic finite automata

Math 300 Introduction to Mathematical Reasoning Autumn 2017 Inverse Functions

Department of Chemical Engineering University of California, Santa Barbara Spring Exercise 2. Due: Thursday, 4/19/09

#26: Number Theory, Part I: Divisibility

Final Review Sheet. B = (1, 1 + 3x, 1 + x 2 ) then 2 + 3x + 6x 2

Worksheet on Derivatives. Dave L. Renfro Drake University November 1, 1999

Voting Systems. High School Circle II. June 4, 2017

Math 31 Lesson Plan. Day 2: Sets; Binary Operations. Elizabeth Gillaspy. September 23, 2011

Physics E-1ax, Fall 2014 Experiment 3. Experiment 3: Force. 2. Find your center of mass by balancing yourself on two force plates.

Star Cluster Photometry and the H-R Diagram

Getting Started with Communications Engineering

Project 2: Using linear systems for numerical solution of boundary value problems

Lab 4: Gauss Gun Conservation of Energy

Student Instruction Sheet: Unit 3, Lesson 3. Solving Quadratic Relations

PHY2048 Physics with Calculus I

Math (P)Review Part I:

2.5 The Fundamental Theorem of Algebra.

Students will explore Stellarium, an open-source planetarium and astronomical visualization software.

Lesson Plan 2 - Middle and High School Land Use and Land Cover Introduction. Understanding Land Use and Land Cover using Google Earth

Conformational Analysis of n-butane

Name Date Partners. Lab 4 - GAUSS' LAW. On all questions, work together as a group.

6. How Functions Work and Are Accessed. Topics: Modules and Functions More on Importing Call Frames

Lesson 3: Advanced Factoring Strategies for Quadratic Expressions

Lecture 9: Elementary Matrices

PHYS133 Lab 7 The HR Diagram

Physics 123 Lab 2: Electric Field Physics 123: Electricity and Magnetism

Q25: Record the wavelength of each colored line according to the scale given.

Mathematica Project 3

LAST TIME..THE COMMAND LINE

Radiation. Laboratory exercise - Astrophysical Radiation Processes. Magnus Gålfalk Stockholm Observatory 2007

Position and Displacement

The Hubble Law & The Structure of the Universe

Using Microsoft Excel

Designing Information Devices and Systems I Fall 2018 Lecture Notes Note 21

Chemistry 5021/8021 Computational Chemistry 3/4 Credits Spring Semester 2004 ( Due 4 / 5 / 04 )

Discrete Structures Proofwriting Checklist

Math 31 Lesson Plan. Day 5: Intro to Groups. Elizabeth Gillaspy. September 28, 2011

Where Is Newton Taking Us? And How Fast?

Uta Bilow, Carsten Bittrich, Constanze Hasterok, Konrad Jende, Michael Kobel, Christian Rudolph, Felix Socher, Julia Woithe

Please read this introductory material carefully; it covers topics you might not yet have seen in class.

MATH REFRESHER ANSWER SHEET (Note: Only this answer sheet and the following graph page will be evaluated)

Guide to Proofs on Discrete Structures

Motion II. Goals and Introduction

Math Refresher Answer Sheet (NOTE: Only this answer sheet and the following graph will be evaluated)

Transcription:

Physics 212E Classical and Modern Physics Spring 2016 1. Introduction VPython Class 2: Functions, Fields, and the dreaded ˆr In our study of electric fields, we start with a single point charge source q source located at some position r source, and we want to know the field created by this source at some observation point r obs. The relationship between the vectors is illustrated in Fig. 1. Note that this figure uses the conventions for axis labeling that is consistent with that of the default view in VPython; in many textbooks, the default view has z going up the page, y to the right, and x coming out of the page. The vector displacement between the source and the observation point is given by Coulomb s law then gives the field at r obs due to the source at r source : r r obs r source. (1) F E = kq ˆr, (2) r2 where ˆr is the unit vector in the direction of ˆr. Your first task will be to write a Python script that will display a point charge at some position r source along with an arrow representing the electric field at some other point r obs. As a specific simple, consider a point charge q source = 3 C on the x-axis with coordinates (3,0,0), and suppose I ask for the field at position with coordinates (3,2,0). Your Python script should display something like Fig. 2, with an an arrow with its tail at the observation point, pointing away from the source, and a length proportional to kq source /r 2 = k3/2 2 = 3k/4 (except your output will be much prettier, and you will be able change your perspective, and zoom in and out). In this exercise we will set k = 1. y ˆr observation point r obs r = r obs r source q source r source x z Figure 1: Vectors associated with the positions of a point charge source and the observation point for an electric field. 1

y 3 2 1 E = 3k 4 ĵ observation point 1 2 3 x z q source Figure 2: Example output. A source charge is located on the x-axis with coordinates (2,0,0), and we show the field vector E with its tail at the observation point with coordinates (2,3,0). 2. Getting Started Reminder: All Programs Python 3.2 IDLE (Python GUI) From the Python shell, select File New Window Now you ve got the IDLE window in which we ll write our scripts. The first line of your program will always (unless otherwise stated) be: from visual import * 3. Python Jargon: Objects and Attributes Before starting on the ultimate task of displaying field vectors, lets review some commands we used last time and learn how to extend them, while introducing some of the language of Python. The command particle = sphere(pos=vector(0,0,0),radius=0.5,color=color.red) creates a sphere object (or equivalently, an object in the sphere class) whose name is particle. We could give it any name we want (e.g., Fred or Ginger). The name is used as a way to identify the object that we just created, and we can create other objects in the same class class with other names. In creating the sphere above, we ve given it certain attributes, namely a position, a radius, and a color. The visual module automatically knows what to do with these attributes in this case, draw the sphere centered at the given position, with the given radius and color. We can also add other attributes to the object by hand, and call them whatever we want. For example: particle.vel = vector(0, 1, -2) 2

adds a vector property of velocity (vel) to the sphere object named particle. This is merely a convenient way for us to keep track of the velocity of this object. The computer doesn t automatically ascribe any special meaning to this attribute; (for example, it doesn t actually affect the speed of the particle in an animation); we have to do that by hand. We could also add this property to the object when we first define it, i.e., particle = sphere(pos=vector(0,0,0),vel=vector(0,1,-2),radius=0.5, color=color.red) Now try the following (one line at a time): print(particle.vel) print(particle.vel.x) print(particle.vel.y) print(particle.vel.z) print(particle.vel+7) print(particle.vel.x+7) If any statements give you errors, try to explain what s wrong with them before deleting them. Since we are eventually going to study the motion of charged particles in electric and magnetic fields, we will need include mass and charge as attributes of some of our objects. Again, we can add them after particle has been defined, using particle.mass = 1 particle.charge = 2 or we can add these attributes to the list when particle is defined. 4. The ˆr vector Now let s get down to the business of calculating and displaying field vectors. We ll start in this section by displaying ˆr vectors. From IDLE, open the file background.py that you can find in the PHYS 211E folder in our departmental public netspace. Save this with a new name in your space. Run this script, and you should see transparent x-y, x-z, and y-z planes that intersect at the origin, along with axis labels. The displayed plane sections are all 10 10 in VPython units. There is no fancy programming here I m just trying to save you some typing. You do not have to use this file as your starting point, but something like this helps me situate the objects we will be displaying in a Cartesian coordinate space. In the rest of this section I m not going to give you explicit commands to type. Rather, I would like you to try to use resources at your disposal, including the handout for VPython Class #1, any scripts you wrote last week, the Help in IDLE, or the online information at http://vpython.org/ contents/docs/primitives.html. None of this should take much time. If you get bogged down, just ask, and I ll give you much more explicit instructions. Create a sphere object at the origin representing a source charge with q = +4. Let s all agree to give this sphere object the name source. 3

The source object has several attributes. Which of these attributes corresponds to the vector r source? Put a temporary print statement into your script to check and make sure that you know how to get the vector r source in terms of your Python variables. (Hint: source is not itself equal to r source.) Define a observation point at (2,0,0) as a Python vector with the name obs. This observation point serves as r obs. (There is no real need to define this as a sphere object; it s just a point, with no radius, color, or charge attributes. But if you want to display it with a colored sphere, that s ok.) Determine a Python expression for the vector r in terms of your source and obs; I suggest calling this r. Determine a Python expression for the vector ˆr in terms of your source and obs; I suggest calling this rhat. Have VPython draw an arrow object at the position obs that represents ˆr. (I use the attribute shaftwidth=0.1). Do you get the arrow you expect? Change your observation point. Does the arrow representing ˆr change appropriately? Change the position of your source charge. Does the arrow representing ˆr change appropriately? 5. Visualizing electric field vectors Modify your previous work so that your script gives the field vector at your observation point. (You may set k = 1.) If you use the previously defined r and rhat it should take a very modest modification to go from an arrow representing ˆr to an arrow representing E. When you have this working, check the effect of varying: the source charge (magnitude and sign), the position of the source charge, and the observation point. Does the the displayed field vector change appropriately? 6. Python functions A Python function is a lot like a math function it takes one or more inputs called arguments, does something with them, and typically gives an output. In your IDLE editor define a function as follows: def myfunction(r): # lots of calculations could go here return 6*r print( myfunction(2) ) 4

Look in the Python shell window (where print results always go). Now try x=7 r=4 print( myfunction(x) ) Is the result what you expected? Explain in your own words what the role of the variable r is in the function. There s more than meets the eye with this function. We didn t tell the function what type of variable r was. Let s make a vector: v = vector(1,2,0) print( myfunction(v) ) Now look at the output. The function myfunction will return a number when the argument r is a number, but it returns a vector when the argument is a vector. This is allowed as long as the operations make sense (and is called dynamic typing). It is one of many ways that Python makes programming easier. Just for fun, see what Python does when you use the character "a" as the input to your function: print( myfunction("a") ) Delete these examples from your IDLE window and then define a function that returns the electric field at an observation point due to a given source charge. This shouldn t involve much more than cutting and pasting existing code. You function should look something like this: def e(obs,source): #your instructions here return... Check to make sure that your function gives the right vectors. 7. The electric field from multiple sources Now consider two charges, illustrated in Fig. 3, like those we studied on the second day of class. Use your electric field function to display the total field vector at the point (2,0,0). Check the field vector at some other points, like (4,0,0), (0,2,0), (0,-2,0),.... +1 q 1 = +10-1 q 2 = 10 Figure 3: Two charges 5

8. Submit your work Make sure to submit your work to my dropbox in facultystaff/m/mligare with the filename vp02_lastname.py when you re done. 9. Extras Reconsider the two charge example, and bump up the charges to ±20. Now create field vectors at 16 equally spaced points around a circle of radius 4. Then add field vectors at equally spaced points around a circle of radius 6. Think about the field lines that would represent the same vector field. Generalize the two charge example to situations in which there are many more sources. What features of Python will make this an easy task. Think about ways in which you might numerically approximate an integral for an line of charge.... 6